<template>
  <div class="container">
    <div class="header">
      <div class="title">{{ superdepartmentName }}部门列表</div>
      <el-button
        style="margin-left: 30px"
        @click.prevent="handleAdd"
        type="primary"
        plain
        size="medium"
        v-permission="{ permission: '创建部门', type: 'disabled' }"
        >创建子部门</el-button
      >
    </div>
    <el-table stripe v-loading="loading" :data="tableData">
      <el-table-column fixed="left" prop="dwbhzdy" label="编号" width="150"></el-table-column>
      <el-table-column
        fixed="left"
        prop="dwmc"
        label="名称"
        width="500"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column fixed="left" prop="dwdh" label="电话" width="200"></el-table-column>
      <el-table-column prop="dwbz" label="备注" min-width="800" :show-overflow-tooltip="true"></el-table-column>
      <!-- <el-table-column prop="dwyxf" label="有效否" width="100">
        <template slot-scope="scope"><p style="color: red;">{{ scope.row.dwyxf | dwyxfFormat }}</p></template>
      </el-table-column> -->
      <el-table-column fixed="right" width="200" label="操作">
        <template slot-scope="scope">
          <el-button @click.prevent="handleEdit(scope.row)" type="primary" plain size="mini">编辑</el-button>
          <el-button
            @click.prevent="handleDelete(scope.row)"
            type="danger"
            plain
            size="mini"
            v-permission="{ permission: '删除部门', type: 'disabled' }"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @current-change="handleCurrentChange"
        :background="true"
        :page-size="pageCount"
        :current-page="currentPage"
        v-if="refreshPagination"
        layout="prev, pager, next, jumper"
        :total="totalNums"
      ></el-pagination>
    </div>
    <!--  创建/编辑部门弹窗  -->
    <department-edit
      v-if="dialogFormVisible"
      :isCreate="isCreate"
      :dialogFormVisible="dialogFormVisible"
      :departmentId="subdepartmentId"
      :isSub="true"
      @dialogClose="dialogClose"
    ></department-edit>
  </div>
</template>

<script>
import department from '@/model/department'
import departmentEdit from './department-edit'

export default {
  components: {
    departmentEdit,
  },
  data() {
    return {
      display: true,
      tableData: [],
      superdepartmentId: 0,
      superdepartmentName: 0,
      subdepartmentId: 0,
      totalNums: 0,
      currentPage: 1,
      pageCount: 10,
      refreshPagination: true,
      dialogFormVisible: false, // 展示弹窗
      isCreate: false,
      root: 1, // 是否是父部门,
      form: {
        is_root: 0,
        parent_id: null,
        dwbhzdy: '',
        dwmc: '',
        dwdh: '',
        dwbz: '',
        dwyxf: 1,
      },
    }
  },
  async created() {
    this.loading = true
    this.superdepartmentId = this.$route.params.id
    this.superdepartmentName = this.$route.query.departmentName
    this.getSubCategories()
    this.loading = false
  },
  filters: {
    dwyxfFormat(val) {
      return val === 1 ? '√' : ''
    },
  },
  watch: {
    display(val) {
      this.form.dwyxf = val ? 1 : 0
    },
  },
  methods: {
    async getSubCategories() {
      const page = this.currentPage - 1
      const count = this.pageCount
      const categories = await department.getSubCategories(page, count, this.superdepartmentId)
      this.tableData = categories.items
      this.totalNums = categories.total
    },
    // 切换table页
    async handleCurrentChange(val) {
      this.currentPage = val
      this.loading = true
      this.getSubCategories()
      this.loading = false
    },
    // 获取所拥有的权限并渲染  由子组件提供
    handleAdd() {
      this.dialogFormVisible = true
      this.isCreate = true
    },
    handleEdit(val) {
      this.subdepartmentId = val.id
      this.isCreate = false
      this.dialogFormVisible = true
    },
    handleDelete(val) {
      let res
      this.$confirm('此操作将永久删除该部门，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        try {
          this.loading = true
          res = await department.deletedepartment(val.id)
        } catch (e) {
          this.loading = false
        }
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.loading = false
          if (this.totalNums % this.pageCount === 1 && this.currentPage !== 1) {
            this.currentPage--
          }
          this.getSubCategories()
          this.$message({
            type: 'success',
            message: `${res.message}`,
          })
        } else {
          this.loading = false
          this.$message.error(`${res.message}`)
        }
      })
    },
    dialogClose() {
      this.dialogFormVisible = false
      this.loading = true
      this.getSubCategories()
      this.loading = false
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  padding: 0 30px;

  .header {
    display: flex;
    align-items: center;

    .title {
      height: 59px;
      line-height: 59px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
    margin: 20px;
  }
}
</style>
